package org.code.boot.controller.boot;

import java.util.List;
import org.springframework.security.access.prepost.PreAuthorize;
import java.util.Map;
import java.util.Collections;
import cn.hutool.core.collection.CollUtil;
import org.code.boot.common.utils.CollectionUtils;
import org.code.boot.common.utils.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.code.boot.common.annotation.Log;
import org.code.boot.common.enums.BusinessType;
import org.code.boot.controller.BaseController;
import org.code.boot.vo.AjaxResult;
import org.code.boot.domain.boot.BizHotelOrder;
import org.code.boot.service.boot.IBizHotelOrderService;
import org.code.boot.vo.TableDataInfo;
import org.code.boot.domain.system.SysUser;
import org.code.boot.service.system.ISysUserService;
import org.code.boot.domain.boot.BizHotel;
import org.code.boot.service.boot.IBizHotelService;

/**
 * 酒店订单Controller
 * 
 * @author coder
 * @date 2025-08-28
 */
@RestController
@RequestMapping("/boot/hotelorder")
public class BizHotelOrderController extends BaseController
{
    @Autowired
    private IBizHotelOrderService bizHotelOrderService;

    @Autowired
    private ISysUserService sysuserService;

    @Autowired
    private IBizHotelService bizhotelService;

    /**
     * 查询酒店订单列表
     */
    @GetMapping("/list")
    public TableDataInfo list(BizHotelOrder bizHotelOrder)
    {
        startPage();
        List<BizHotelOrder> list = bizHotelOrderService.selectBizHotelOrderList(bizHotelOrder);
        list = buildDetailList(list);
        return getDataTable(list);
    }



    /**
     * 获取酒店订单下拉选项列表
     */
    @GetMapping("/options")
    public AjaxResult getOptions()
    {
        List<BizHotelOrder> list = bizHotelOrderService.selectBizHotelOrderList(new BizHotelOrder());
        return success(list);
    }

    /**
     * 获取当前用户的酒店订单列表
     */
    @GetMapping("/userOrders")
    public AjaxResult getUserHotelOrders()
    {
        BizHotelOrder queryParam = new BizHotelOrder();
        queryParam.setUserId(getUserId());
        List<BizHotelOrder> list = bizHotelOrderService.selectBizHotelOrderList(queryParam);
        list = buildDetailList(list);
        return success(list);
    }

    /**
     * 获取酒店订单详细信息
     */
    @GetMapping(value = "/{hotelOrderId}")
    public AjaxResult getInfo(@PathVariable("hotelOrderId") Long hotelOrderId)
    {
        return success(bizHotelOrderService.selectBizHotelOrderByHotelOrderId(hotelOrderId));
    }

    /**
     * 新增酒店订单
     */
    @Log(title = "酒店订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizHotelOrder bizHotelOrder)
    {
        bizHotelOrder.setCreateBy(getNickname());
        return toAjax(bizHotelOrderService.insertBizHotelOrder(bizHotelOrder));
    }

    /**
     * 创建酒店订单（包含库存更新）
     */
    @Log(title = "酒店订单", businessType = BusinessType.INSERT)
    @PostMapping("/create")
    public AjaxResult createOrder(@RequestBody BizHotelOrder bizHotelOrder)
    {
        bizHotelOrder.setCreateBy(getNickname());
        bizHotelOrder.setUserId(getUserId());
        return toAjax(bizHotelOrderService.createHotelOrder(bizHotelOrder));
    }

    /**
     * 修改酒店订单
     */
    @Log(title = "酒店订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizHotelOrder bizHotelOrder)
    {
        bizHotelOrder.setUpdateBy(getNickname());
        return toAjax(bizHotelOrderService.updateBizHotelOrder(bizHotelOrder));
    }

    /**
     * 删除酒店订单
     */
    @Log(title = "酒店订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{hotelOrderIds}")
    public AjaxResult remove(@PathVariable Long[] hotelOrderIds)
    {
        return toAjax(bizHotelOrderService.deleteBizHotelOrderByHotelOrderIds(hotelOrderIds));
    }

    /**
     * 构建详细信息列表
     * 
     * @param list 列表数据
     * @return 构建后的列表数据
     */
    private List<BizHotelOrder> buildDetailList(List<BizHotelOrder> list) {
        if (CollUtil.isEmpty(list)) {
            return Collections.emptyList();
        }
        
        // 获取用户Map
        Map<Long, SysUser> userIdMap = sysuserService.getSysUserMap(
                CollectionUtils.convertSet(list, BizHotelOrder::getUserId));
        
        // 获取酒店Map
        Map<Long, BizHotel> hotelIdMap = bizhotelService.getBizHotelMap(
                CollectionUtils.convertSet(list, BizHotelOrder::getHotelId));
        
        // 拼接结果
        list.forEach(item -> {
            // 拼接用户名称
            MapUtils.findAndThen(userIdMap, item.getUserId(), 
                userIdItem -> item.setUserIdName(userIdItem.getNickName()));
            // 拼接酒店名称
            MapUtils.findAndThen(hotelIdMap, item.getHotelId(), 
                hotelIdItem -> item.setHotelIdName(hotelIdItem.getHotelName()));
        });
        
        return list;
    }
}
